package com.marui.ch3;

import java.util.concurrent.atomic.AtomicReference;

/**
 *
 *类说明：演示引用类型的原子操作类
 */
public class UseAtomicReference {
	
	static AtomicReference<UserInfo> userRef = new AtomicReference<UserInfo>();
	
    public static void main(String[] args) {
        //要修改的实体的实例AtomicMarkableReference
        UserInfo user = new UserInfo("Mark", 15);
        userRef.set(user);
        //要变化的新实例
        UserInfo updateUser = new UserInfo("Bill", 17);
        //要变化的新实例
        UserInfo user2 = new UserInfo("Bill2", 127);
        userRef.compareAndSet(user2, updateUser);
        System.out.println(userRef.get().getName());
        System.out.println(user.getName());
    }
    
    //定义一个实体类
    static class UserInfo {
        private String name;
        private int age;
        public UserInfo(String name, int age) {
            this.name = name;
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public int getAge() {
            return age;
        }
    }

}
